
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>9.判断（if）语句 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="判断（if）语句目标
开发中的应用场景
if 语句体验
if 语句进阶
综合应用

01. 开发中的应用场景生活中的判断几乎是无所不在的，我们每天都在做各种各样的选择，如果这样？如果那样？……


,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="9.判断（if）语句 - fubaisen"/>
    <meta name="twitter:description" content="判断（if）语句目标
开发中的应用场景
if 语句体验
if 语句进阶
综合应用

01. 开发中的应用场景生活中的判断几乎是无所不在的，我们每天都在做各种各样的选择，如果这样？如果那样？……


,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="9.判断（if）语句 - fubaisen"/>
    <meta property="og:description" content="判断（if）语句目标
开发中的应用场景
if 语句体验
if 语句进阶
综合应用

01. 开发中的应用场景生活中的判断几乎是无所不在的，我们每天都在做各种各样的选择，如果这样？如果那样？……


,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">9.判断（if）语句</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">9.判断（if）语句</h1>
        <div class="stuff">
            <span>十二月 23, 2021</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/python%E5%AD%A6%E4%B9%A0/" rel="tag">python学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="判断（if）语句"><a href="#判断（if）语句" class="headerlink" title="判断（if）语句"></a>判断（if）语句</h1><h2 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h2><ul>
<li>开发中的应用场景</li>
<li>if 语句体验</li>
<li>if 语句进阶</li>
<li>综合应用</li>
</ul>
<h2 id="01-开发中的应用场景"><a href="#01-开发中的应用场景" class="headerlink" title="01. 开发中的应用场景"></a>01. 开发中的应用场景</h2><p>生活中的判断几乎是无所不在的，我们每天都在做各种各样的选择，如果这样？如果那样？……</p>
<p><img src="https://s2.loli.net/2023/03/15/rYhwx9yJaSeDP78.jpg"></p>
<p><img src="https://s2.loli.net/2023/03/15/8cElrgAQXRSJFVC.png" alt="image-20211216215930591"></p>
<h3 id="程序中的判断"><a href="#程序中的判断" class="headerlink" title="程序中的判断"></a>程序中的判断</h3><p><img src="https://s2.loli.net/2023/03/15/Mv39Q1OaR8hIswP.png"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 今天发工资:</span><br><span class="line"></span><br><span class="line">    先还信用卡的钱</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> 有剩余:</span><br><span class="line"></span><br><span class="line">        又可以happy了，O(∩_∩)O哈哈~</span><br><span class="line"></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line">        噢，no。。。还的等<span class="number">30</span>天</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line"></span><br><span class="line">    盼着发工资</span><br></pre></td></tr></table></figure>

<h3 id="判断的定义"><a href="#判断的定义" class="headerlink" title="判断的定义"></a>判断的定义</h3><ul>
<li>如果 <strong>条件满足</strong>，才能做某件事情，</li>
<li>如果 <strong>条件不满足</strong>，就做另外一件事情，或者什么也不做</li>
</ul>
<blockquote>
<p>正是因为有了判断，才使得程序世界丰富多彩，充满变化！</p>
<p><strong>判断语句</strong> 又被称为 “分支语句”，正是因为有了判断，才让程序有了很多的分支</p>
</blockquote>
<h2 id="02-if-语句体验"><a href="#02-if-语句体验" class="headerlink" title="02. if 语句体验"></a>02. if 语句体验</h2><h3 id="2-1-if-判断语句基本语法"><a href="#2-1-if-判断语句基本语法" class="headerlink" title="2.1 if 判断语句基本语法"></a>2.1 if 判断语句基本语法</h3><p>在 <code>Python</code> 中，<strong>if 语句</strong> 就是用来进行判断的，格式如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 要判断的条件:</span><br><span class="line">    条件成立时，要做的事情</span><br><span class="line">    ……</span><br></pre></td></tr></table></figure>

<blockquote>
<p>注意：代码的缩进为一个 <code>tab</code> 键，或者 <strong>4</strong> 个空格 —— <strong>建议使用空格</strong></p>
<ul>
<li>在 Python 开发中，Tab 和空格不要混用！</li>
</ul>
</blockquote>
<p><strong>我们可以把整个 if 语句看成一个完整的代码块</strong></p>
<p><img src="https://s2.loli.net/2023/03/15/Mv39Q1OaR8hIswP.png"></p>
<h3 id="2-2-判断语句演练-——-判断年龄"><a href="#2-2-判断语句演练-——-判断年龄" class="headerlink" title="2.2 判断语句演练 —— 判断年龄"></a>2.2 判断语句演练 —— 判断年龄</h3><p><strong>需求</strong></p>
<ol>
<li>定义一个整数变量记录年龄</li>
<li>判断是否满 18 岁 （**&gt;&#x3D;**）</li>
<li>如果满 18 岁，允许进网吧嗨皮</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 定义年龄变量</span></span><br><span class="line">age = <span class="number">18</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 判断是否满 18 岁</span></span><br><span class="line"><span class="comment"># if 语句以及缩进部分的代码是一个完整的代码块</span></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;可以进网吧嗨皮……&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 思考！- 无论条件是否满足都会执行</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;这句代码什么时候执行?&quot;</span>)</span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：</p>
<ul>
<li><code>if</code> 语句以及缩进部分是一个 <strong>完整的代码块</strong></li>
</ul>
<h3 id="2-3-else-处理条件不满足的情况"><a href="#2-3-else-处理条件不满足的情况" class="headerlink" title="2.3 else 处理条件不满足的情况"></a>2.3 else 处理条件不满足的情况</h3><p><strong>思考</strong></p>
<p>在使用 <code>if</code> 判断时，只能做到满足条件时要做的事情。那如果需要在 <strong>不满足条件的时候</strong>，做某些事情，该如何做呢？</p>
<p><strong>答案</strong></p>
<p><code>else</code>，格式如下：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 要判断的条件:</span><br><span class="line">    条件成立时，要做的事情</span><br><span class="line">    ……</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    条件不成立时，要做的事情</span><br><span class="line">    ……</span><br></pre></td></tr></table></figure>

<p><strong>注意</strong>：</p>
<ul>
<li><code>if</code> 和 <code>else</code> 语句以及各自的缩进部分共同是一个 <strong>完整的代码块</strong></li>
</ul>
<h3 id="2-4-判断语句演练-——-判断年龄改进"><a href="#2-4-判断语句演练-——-判断年龄改进" class="headerlink" title="2.4 判断语句演练 —— 判断年龄改进"></a>2.4 判断语句演练 —— 判断年龄改进</h3><p><strong>需求</strong></p>
<ol>
<li>输入用户年龄</li>
<li>判断是否满 18 岁 （**&gt;&#x3D;**）</li>
<li>如果满 18 岁，允许进网吧嗨皮</li>
<li>如果未满 18 岁，提示回家写作业</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 1. 输入用户年龄</span></span><br><span class="line">age = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;今年多大了？&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 2. 判断是否满 18 岁</span></span><br><span class="line"><span class="comment"># if 语句以及缩进部分的代码是一个完整的语法块</span></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">18</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;可以进网吧嗨皮……&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;你还没长大，应该回家写作业！&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 3. 思考！- 无论条件是否满足都会执行</span></span><br><span class="line"><span class="built_in">print</span>(<span class="string">&quot;这句代码什么时候执行?&quot;</span>)</span><br></pre></td></tr></table></figure>

<p><img src="https://s2.loli.net/2023/03/15/ALR8cepowqENh5C.png"></p>
<p><img src="https://s2.loli.net/2023/03/15/jn7RWLKHAz54rqw.png"></p>
<h2 id="03-逻辑运算"><a href="#03-逻辑运算" class="headerlink" title="03. 逻辑运算"></a>03. 逻辑运算</h2><ul>
<li>在程序开发中，通常 <strong>在判断条件时</strong>，会需要同时判断多个条件</li>
<li>只有多个条件都满足，才能够执行后续代码，这个时候需要使用到 <strong>逻辑运算符</strong></li>
<li><strong>逻辑运算符</strong> 可以把 <strong>多个条件</strong> 按照 <strong>逻辑</strong> 进行 <strong>连接</strong>，变成 <strong>更复杂的条件</strong></li>
<li>Python 中的 <strong>逻辑运算符</strong> 包括：<strong>与 and</strong>／<strong>或 or</strong>／<strong>非 not</strong> 三种</li>
</ul>
<h3 id="3-1-and"><a href="#3-1-and" class="headerlink" title="3.1 and"></a>3.1 <code>and</code></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">条件1 and 条件2</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>与</strong>／<strong>并且</strong></li>
<li>两个条件同时满足，返回 <code>True</code></li>
<li>只要有一个不满足，就返回 <code>False</code></li>
</ul>
<table>
<thead>
<tr>
<th align="center">条件 1</th>
<th align="center">条件 2</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">成立</td>
<td align="center">成立</td>
<td align="center">成立</td>
</tr>
<tr>
<td align="center">成立</td>
<td align="center">不成立</td>
<td align="center">不成立</td>
</tr>
<tr>
<td align="center">不成立</td>
<td align="center">成立</td>
<td align="center">不成立</td>
</tr>
<tr>
<td align="center">不成立</td>
<td align="center">不成立</td>
<td align="center">不成立</td>
</tr>
</tbody></table>
<h3 id="3-2-or"><a href="#3-2-or" class="headerlink" title="3.2 or"></a>3.2 <code>or</code></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">条件1 or 条件2</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>或</strong>／<strong>或者</strong></li>
<li>两个条件只要有一个满足，返回 <code>True</code></li>
<li>两个条件都不满足，返回 <code>False</code></li>
</ul>
<table>
<thead>
<tr>
<th align="center">条件 1</th>
<th align="center">条件 2</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">成立</td>
<td align="center">成立</td>
<td align="center">成立</td>
</tr>
<tr>
<td align="center">成立</td>
<td align="center">不成立</td>
<td align="center">成立</td>
</tr>
<tr>
<td align="center">不成立</td>
<td align="center">成立</td>
<td align="center">成立</td>
</tr>
<tr>
<td align="center">不成立</td>
<td align="center">不成立</td>
<td align="center">不成立</td>
</tr>
</tbody></table>
<h3 id="3-3-not"><a href="#3-3-not" class="headerlink" title="3.3 not"></a>3.3 <code>not</code></h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">not 条件</span><br></pre></td></tr></table></figure>

<ul>
<li><strong>非</strong>／<strong>不是</strong></li>
</ul>
<table>
<thead>
<tr>
<th align="center">条件</th>
<th align="center">结果</th>
</tr>
</thead>
<tbody><tr>
<td align="center">成立</td>
<td align="center">不成立</td>
</tr>
<tr>
<td align="center">不成立</td>
<td align="center">成立</td>
</tr>
</tbody></table>
<h4 id="逻辑运算演练"><a href="#逻辑运算演练" class="headerlink" title="逻辑运算演练"></a>逻辑运算演练</h4><ol>
<li>练习1: 定义一个整数变量 <code>age</code>，编写代码判断年龄是否正确<ul>
<li>要求人的年龄在 0-120 之间</li>
</ul>
</li>
<li>练习2: 定义两个整数变量 <code>python_score</code>、<code>c_score</code>，编写代码判断成绩<ul>
<li>要求只要有一门成绩 &gt; 60 分就算合格</li>
</ul>
</li>
<li>练习3: 定义一个布尔型变量 <code>is_employee</code>，编写代码判断是否是本公司员工<ul>
<li>如果不是提示不允许入内</li>
</ul>
</li>
</ol>
<p>答案 1：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 练习1: 定义一个整数变量 age，编写代码判断年龄是否正确</span></span><br><span class="line">age = <span class="number">100</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 要求人的年龄在 0-120 之间</span></span><br><span class="line"><span class="keyword">if</span> age &gt;= <span class="number">0</span> <span class="keyword">and</span> age &lt;= <span class="number">120</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;年龄正确&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;年龄不正确&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>答案 2：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 练习2: 定义两个整数变量 python_score、c_score，编写代码判断成绩</span></span><br><span class="line">python_score = <span class="number">50</span></span><br><span class="line">c_score = <span class="number">50</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 要求只要有一门成绩 &gt; 60 分就算合格</span></span><br><span class="line"><span class="keyword">if</span> python_score &gt; <span class="number">60</span> <span class="keyword">or</span> c_score &gt; <span class="number">60</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;考试通过&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;再接再厉！&quot;</span>)</span><br></pre></td></tr></table></figure>

<p>答案 3：</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 练习3: 定义一个布尔型变量 `is_employee`，编写代码判断是否是本公司员工</span></span><br><span class="line">is_employee = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果不是提示不允许入内</span></span><br><span class="line"><span class="comment"># 在开发中，通常希望某个条件不满足时，执行一些代码，可以使用 not</span></span><br><span class="line"><span class="comment"># 另外，如果需要拼接复杂的逻辑计算条件，同样也有可能使用到 not</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> <span class="keyword">not</span> is_employee:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;非公勿内&quot;</span>)</span><br></pre></td></tr></table></figure>

<h2 id="04-if-语句进阶"><a href="#04-if-语句进阶" class="headerlink" title="04. if 语句进阶"></a>04. if 语句进阶</h2><h3 id="4-1-elif"><a href="#4-1-elif" class="headerlink" title="4.1 elif"></a>4.1 <code>elif</code></h3><ul>
<li>在开发中，使用 <code>if</code> 可以 <strong>判断条件</strong></li>
<li>使用 <code>else</code> 可以处理 <strong>条件不成立</strong> 的情况</li>
<li>但是，如果希望 <strong>再增加一些条件</strong>，<strong>条件不同，需要执行的代码也不同</strong> 时，就可以使用 <code>elif</code> </li>
<li>语法格式如下：</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件<span class="number">1</span>:</span><br><span class="line">    条件<span class="number">1</span>满足执行的代码</span><br><span class="line">    ……</span><br><span class="line"><span class="keyword">elif</span> 条件<span class="number">2</span>:</span><br><span class="line">    条件<span class="number">2</span>满足时，执行的代码</span><br><span class="line">    ……</span><br><span class="line"><span class="keyword">elif</span> 条件<span class="number">3</span>:</span><br><span class="line">    条件<span class="number">3</span>满足时，执行的代码</span><br><span class="line">    ……</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    以上条件都不满足时，执行的代码</span><br><span class="line">    ……</span><br></pre></td></tr></table></figure>

<ul>
<li>对比逻辑运算符的代码</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件<span class="number">1</span> <span class="keyword">and</span> 条件<span class="number">2</span>:</span><br><span class="line">    条件<span class="number">1</span>满足 并且 条件<span class="number">2</span>满足 执行的代码</span><br><span class="line">    ……</span><br></pre></td></tr></table></figure>

<p><strong>注意</strong></p>
<ol>
<li><code>elif</code> 和 <code>else</code> 都必须和 <code>if</code> 联合使用，而不能单独使用</li>
<li>可以将 <code>if</code>、<code>elif</code> 和 <code>else</code> 以及各自缩进的代码，看成一个 <strong>完整的代码块</strong></li>
</ol>
<h4 id="elif-演练-——-女友的节日"><a href="#elif-演练-——-女友的节日" class="headerlink" title="elif 演练 —— 女友的节日"></a>elif 演练 —— 女友的节日</h4><p><strong>需求</strong></p>
<ol>
<li>定义 <code>holiday_name</code> 字符串变量记录节日名称</li>
<li>如果是 <strong>情人节</strong> 应该 <strong>买玫瑰</strong>／<strong>看电影</strong></li>
<li>如果是 <strong>平安夜</strong> 应该 <strong>买苹果</strong>／<strong>吃大餐</strong></li>
<li>如果是 <strong>生日</strong> 应该 <strong>买蛋糕</strong></li>
<li>其他的日子每天都是节日啊……</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">holiday_name = &quot;平安夜&quot;</span><br><span class="line"></span><br><span class="line">if holiday_name == &quot;情人节&quot;:</span><br><span class="line">    print(&quot;买玫瑰&quot;)</span><br><span class="line">    print(&quot;看电影&quot;)</span><br><span class="line">elif holiday_name == &quot;平安夜&quot;:</span><br><span class="line">    print(&quot;买苹果&quot;)</span><br><span class="line">    print(&quot;吃大餐&quot;)</span><br><span class="line">elif holiday_name == &quot;生日&quot;:</span><br><span class="line">    print(&quot;买蛋糕&quot;)</span><br><span class="line">else:</span><br><span class="line">    print(&quot;每天都是节日啊……&quot;)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="4-2-if-的嵌套"><a href="#4-2-if-的嵌套" class="headerlink" title="4.2 if 的嵌套"></a>4.2 <code>if</code> 的嵌套</h3><p><img src="https://s2.loli.net/2023/03/15/8cElrgAQXRSJFVC.png"></p>
<blockquote>
<p><strong>elif</strong> 的应用场景是：<strong>同时</strong> 判断 <strong>多个条件</strong>，所有的条件是 <strong>平级</strong> 的</p>
</blockquote>
<ul>
<li>在开发中，使用 <code>if</code> 进行条件判断，如果希望 <strong>在条件成立的执行语句中</strong> 再 <strong>增加条件判断</strong>，就可以使用 <strong>if 的嵌套</strong></li>
<li><strong>if 的嵌套</strong> 的应用场景就是：<strong>在之前条件满足的前提下，再增加额外的判断</strong></li>
<li><strong>if 的嵌套</strong> 的语法格式，<strong>除了缩进之外</strong> 和之前的没有区别</li>
<li>语法格式如下：</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> 条件 <span class="number">1</span>:</span><br><span class="line">    条件 <span class="number">1</span> 满足执行的代码</span><br><span class="line">    ……</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">if</span> 条件 <span class="number">1</span> 基础上的条件 <span class="number">2</span>:</span><br><span class="line">        条件 <span class="number">2</span> 满足时，执行的代码</span><br><span class="line">        ……    </span><br><span class="line">        </span><br><span class="line">    <span class="comment"># 条件 2 不满足的处理</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        条件 <span class="number">2</span> 不满足时，执行的代码</span><br><span class="line">        </span><br><span class="line"><span class="comment"># 条件 1 不满足的处理</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    条件<span class="number">1</span> 不满足时，执行的代码</span><br><span class="line">    ……</span><br></pre></td></tr></table></figure>

<h4 id="if-的嵌套-演练-——-火车站安检"><a href="#if-的嵌套-演练-——-火车站安检" class="headerlink" title="if 的嵌套 演练 —— 火车站安检"></a>if 的嵌套 演练 —— 火车站安检</h4><p><strong>需求</strong></p>
<ol>
<li>定义布尔型变量 <code>has_ticket</code> 表示是否有车票</li>
<li>定义整型变量 <code>knife_length</code> 表示刀的长度，单位：厘米</li>
<li>首先检查是否有车票，如果有，才允许进行 <strong>安检</strong></li>
<li>安检时，需要检查刀的长度，判断是否超过 20 厘米<ul>
<li>如果超过 20 厘米，提示刀的长度，不允许上车</li>
<li>如果不超过 20 厘米，安检通过</li>
</ul>
</li>
<li>如果没有车票，不允许进门</li>
</ol>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 定义布尔型变量 has_ticket 表示是否有车票</span></span><br><span class="line">has_ticket = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 定义整数型变量 knife_length 表示刀的长度，单位：厘米</span></span><br><span class="line">knife_length = <span class="number">20</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 首先检查是否有车票，如果有，才允许进行 安检</span></span><br><span class="line"><span class="keyword">if</span> has_ticket:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;有车票，可以开始安检...&quot;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 安检时，需要检查刀的长度，判断是否超过 20 厘米</span></span><br><span class="line">    <span class="comment"># 如果超过 20 厘米，提示刀的长度，不允许上车</span></span><br><span class="line">    <span class="keyword">if</span> knife_length &gt;= <span class="number">20</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;不允许携带 %d 厘米长的刀上车&quot;</span> % knife_length)</span><br><span class="line">    <span class="comment"># 如果不超过 20 厘米，安检通过</span></span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;安检通过，祝您旅途愉快……&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 如果没有车票，不允许进门</span></span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;大哥，您要先买票啊&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="05-综合应用-——-石头剪刀布"><a href="#05-综合应用-——-石头剪刀布" class="headerlink" title="05. 综合应用 —— 石头剪刀布"></a>05. 综合应用 —— 石头剪刀布</h2><p><strong>目标</strong></p>
<ol>
<li>强化 <strong>多个条件</strong> 的 <strong>逻辑运算</strong></li>
<li>体会 <code>import</code> 导入模块（“工具包”）的使用</li>
</ol>
<p><strong>需求</strong></p>
<ol>
<li>从控制台输入要出的拳 —— 石头（1）／剪刀（2）／布（3）</li>
<li>电脑 <strong>随机</strong> 出拳 —— 先假定电脑只会出石头，完成整体代码功能</li>
<li>比较胜负</li>
</ol>
<table>
<thead>
<tr>
<th align="center">序号</th>
<th align="center">规则</th>
</tr>
</thead>
<tbody><tr>
<td align="center">1</td>
<td align="center">石头 胜 剪刀</td>
</tr>
<tr>
<td align="center">2</td>
<td align="center">剪刀 胜 布</td>
</tr>
<tr>
<td align="center">3</td>
<td align="center">布 胜 石头</td>
</tr>
</tbody></table>
<h3 id="5-1-基础代码实现"><a href="#5-1-基础代码实现" class="headerlink" title="5.1 基础代码实现"></a>5.1 基础代码实现</h3><ul>
<li>先 <strong>假定电脑就只会出石头</strong>，完成整体代码功能</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 从控制台输入要出的拳 —— 石头（1）／剪刀（2）／布（3）</span></span><br><span class="line">player = <span class="built_in">int</span>(<span class="built_in">input</span>(<span class="string">&quot;请出拳 石头（1）／剪刀（2）／布（3）：&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 电脑 随机 出拳 - 假定电脑永远出石头</span></span><br><span class="line">computer = <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 比较胜负</span></span><br><span class="line"><span class="comment"># 如果条件判断的内容太长，可以在最外侧的条件增加一对大括号</span></span><br><span class="line"><span class="keyword">if</span> (player == <span class="number">1</span> <span class="keyword">and</span> computer == <span class="number">2</span>) <span class="keyword">or</span> (player == <span class="number">2</span> <span class="keyword">and</span> computer == <span class="number">3</span>) <span class="keyword">or</span> (player == <span class="number">3</span> <span class="keyword">and</span> computer == <span class="number">1</span>):</span><br><span class="line"><span class="comment"># 再在每一个条件之间，使用回车，PyCharm 可以自动增加 8 个空格</span></span><br><span class="line"><span class="keyword">if</span> ((player == <span class="number">1</span> <span class="keyword">and</span> computer == <span class="number">2</span>) <span class="keyword">or</span></span><br><span class="line">        (player == <span class="number">2</span> <span class="keyword">and</span> computer == <span class="number">3</span>) <span class="keyword">or</span></span><br><span class="line">        (player == <span class="number">3</span> <span class="keyword">and</span> computer == <span class="number">1</span>)):</span><br><span class="line"></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;噢耶！！！电脑弱爆了！！！&quot;</span>)</span><br><span class="line"><span class="keyword">elif</span> player == computer:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;心有灵犀，再来一盘！&quot;</span>)</span><br><span class="line"><span class="keyword">else</span>:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;不行，我要和你决战到天亮！&quot;</span>)</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="5-2-随机数的处理"><a href="#5-2-随机数的处理" class="headerlink" title="5.2 随机数的处理"></a>5.2 随机数的处理</h3><ul>
<li>在 <code>Python</code> 中，要使用随机数，首先需要导入 <strong>随机数</strong> 的 <strong>模块</strong> —— “工具包”</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> random</span><br></pre></td></tr></table></figure>

<ul>
<li><p>导入模块后，可以直接在 <strong>模块名称</strong> 后面敲一个 <code>.</code> 然后按 <code>Tab</code> 键，会提示该模块中包含的所有函数</p>
</li>
<li><p><code>random.randint(a, b)</code> ，返回 <code>[a, b]</code> 之间的整数，包含 <code>a</code> 和 <code>b</code></p>
</li>
<li><p>例如：</p>
</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">random.randint(<span class="number">12</span>, <span class="number">20</span>)  <span class="comment"># 生成的随机数n: 12 &lt;= n &lt;= 20   </span></span><br><span class="line">random.randint(<span class="number">20</span>, <span class="number">20</span>)  <span class="comment"># 结果永远是 20   </span></span><br><span class="line">random.randint(<span class="number">20</span>, <span class="number">10</span>)  <span class="comment"># 该语句是错误的，下限必须小于上限</span></span><br></pre></td></tr></table></figure>


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
